package cn.kgc.springcloud.web.service.impl;

import cn.kgc.springcloud.web.client.CityClient;
import cn.kgc.springcloud.web.client.ClazzClient;
import cn.kgc.springcloud.web.client.GroupClient;
import cn.kgc.springcloud.web.client.TagClient;
import cn.kgc.springcloud.web.entity.*;
import cn.kgc.springcloud.web.form.StudentForm;
import cn.kgc.springcloud.web.mapper.StudentTagMapper;
import cn.kgc.springcloud.web.response.ResponseResult;
import cn.kgc.springcloud.web.utils.DateUtil;
import cn.kgc.springcloud.web.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springcloud.web.service.StudentService;
import cn.kgc.springcloud.web.mapper.StudentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author mengshun
* @description 针对表【t_student】的数据库操作Service实现
* @createDate 2024-07-29 09:52:57
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService{

    @Resource
    private CityClient cityClient;
    @Resource
    private ClazzClient clazzClient;
    @Resource
    private GroupClient groupClient;
    @Resource
    private TagClient tagClient;

    @Resource
    private StudentTagMapper studentTagMapper;

    @Override
    public ResponseResult studentList() {

        List<Student> students = baseMapper.selectList(null);

        List<StudentVO> collect = students.stream().map(student -> {

            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student,studentVO);

            //  城市信息
            Integer cityid = student.getCityid();
            City oneCity = cityClient.getOneCity(cityid);
            studentVO.setCity(oneCity);

            // 小组信息
            Integer groupid = student.getGroupid();
            Group oneGroup = groupClient.getOneGroup(groupid);
            studentVO.setGroup(oneGroup);

            // 班级信息
            Integer clazzid = student.getClazzid();
            Clazz oneClazz = clazzClient.getOneClazz(clazzid);
            studentVO.setClazz(oneClazz);

            // 标签列表
            Integer sid = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid,sid);

            List<Integer> tIds = studentTagMapper.selectList(lambda)
                    .stream()
                    .map(studentTag -> studentTag.getTid())
                    .collect(Collectors.toList());

            //  根据tid  查询标签的详细信息
            List<Tag> tags = tIds.stream().map(tid -> {
                Tag oneTag = tagClient.getOneTag(tid);

                return oneTag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);

            return studentVO;
        }).collect(Collectors.toList());

        return ResponseResult.success().data(collect);
    }

    @Override
    public ResponseResult addStudent(StudentForm studentForm) {

        //  1.添加数据到student表
        Student student = new Student();
        BeanUtils.copyProperties(studentForm,student);

        // 根据生日信息  计算学生的年龄  属相  星座
        Date bir = student.getBir();
        String attr = DateUtil.getYear(bir);
        int age = DateUtil.getAge(bir);
        String starts = DateUtil.getConstellation(bir);

        student.setAge(age);
        student.setAttr(attr);
        student.setStarts(starts);

        baseMapper.insert(student);

        // 添加完毕 获取自增的主键
        Integer sid = student.getId();
        List<Integer> tagIds = studentForm.getTagIds();

        // 2.添加学生标签信息 student_tag   sid
        tagIds.stream().forEach(tid->{
            StudentTag studentTag = new StudentTag(sid,tid);
            studentTagMapper.insert(studentTag);
        });

        //  响应数据时   需要将 student  ->  studentVO
        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student,studentVO);

        // 1.  根据城市id  查询城市信息
        Integer cityid = student.getCityid();
        City oneCity = cityClient.getOneCity(cityid);
        studentVO.setCity(oneCity);

        // 2.获取小组信息
        Integer groupid = student.getGroupid();
        Group oneGroup = groupClient.getOneGroup(groupid);
        studentVO.setGroup(oneGroup);

        // 3.获取班级信息
        Integer clazzid = student.getClazzid();
        Clazz oneClazz = clazzClient.getOneClazz(clazzid);
        studentVO.setClazz(oneClazz);

        // 4.获取标签列表
        List<Tag> tags = tagIds.stream().map(tid -> {
            Tag oneTag = tagClient.getOneTag(tid);
            return oneTag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);


        return ResponseResult.success().data(studentVO);
    }
}




